package com.ruoyi.financial.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.common.config.RuoYiConfig;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.financial.domain.FCar;
import com.ruoyi.financial.service.IFCarService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆登记Controller
 * 
 * @author xunzhe
 * @date 2024-03-10
 */
@Controller
@RequestMapping("/financial/fCar")
public class FCarController extends BaseController
{
    private String prefix = "financial/fCar";

    @Autowired
    private IFCarService fCarService;

    @RequiresPermissions("financial:fCar:view")
    @GetMapping()
    public String fCar()
    {
        return prefix + "/fCar";
    }

    /**
     * 查询车辆登记列表
     */
    @RequiresPermissions("financial:fCar:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FCar fCar)
    {
        startPage();
        List<FCar> list = fCarService.selectFCarList(fCar);
        return getDataTable(list);
    }

    /**
     * 导出车辆登记列表
     */
    @RequiresPermissions("financial:fCar:export")
    @Log(title = "车辆登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FCar fCar)
    {
        List<FCar> list = fCarService.selectFCarList(fCar);
        Map returnMap = new HashMap();
        List dataList = new ArrayList();
        for(int i=0;i<list.size();i++){
            Map map = new HashMap();
            map.put("xuhao",i+1);
            map.put("deptName",list.get(i).getDeptName());
            map.put("carModel",list.get(i).getCarModel());
            map.put("carNumber",list.get(i).getCarNumber());
            map.put("laiyuan",list.get(i).getLaiyuan());
            map.put("purchaseTime",list.get(i).getPurchaseTime());
            map.put("purchasePrice",list.get(i).getPurchasePrice());
            map.put("remark",list.get(i).getRemark());
            dataList.add(map);
        }
        returnMap.put("list",dataList);
        returnMap.put("createByDept",getSysUser().getDept().getDeptName());
        try {
            TemplateExportParams params = new TemplateExportParams(RuoYiConfig.getUploadPath() + "/excelmoban/car.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, returnMap);
            String fileName = "公务用车实物管理台账.xlsx";
            String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
            File desc = new File(downloadPath);
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(downloadPath);
            workbook.write(fos);
            fos.close();
            return AjaxResult.success(fileName);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增车辆登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆登记
     */
    @RequiresPermissions("financial:fCar:add")
    @Log(title = "车辆登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FCar fCar)
    {
        FCar checkCar = fCarService.checkCarNumber(fCar.getCarNumber());
        if(checkCar != null){
            return AjaxResult.warn("车牌号重复");
        }

        fCar.setCreateBy(getLoginName());
        fCar.setDeptId(getSysUser().getDeptId());
        return toAjax(fCarService.insertFCar(fCar));
    }

    /**
     * 修改车辆登记
     */
    @RequiresPermissions("financial:fCar:edit")
    @GetMapping("/edit/{carId}")
    public String edit(@PathVariable("carId") Long carId, ModelMap mmap)
    {
        FCar fCar = fCarService.selectFCarByCarId(carId);
        mmap.put("fCar", fCar);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆登记
     */
    @RequiresPermissions("financial:fCar:edit")
    @Log(title = "车辆登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FCar fCar)
    {
        FCar checkCar = fCarService.checkCarNumber(fCar.getCarNumber());
        if(checkCar != null){
            if(fCar.getCarId().longValue() != checkCar.getCarId().longValue()){
                return AjaxResult.warn("车牌号重复");
            }
        }
        return toAjax(fCarService.updateFCar(fCar));
    }

    /**
     * 删除车辆登记
     */
    @RequiresPermissions("financial:fCar:remove")
    @Log(title = "车辆登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fCarService.deleteFCarByCarIds(ids));
    }

    @RequiresPermissions("financial:fCar:detail")
    @GetMapping("/view/{carId}")
    public String fCar(@PathVariable("carId") Long carId, ModelMap mmap)
    {
        FCar fCar = fCarService.selectFCarByCarId(carId);
        mmap.put("fCar", fCar);
        return prefix + "/view";
    }


}
